package com.lh.api.controller;

import com.lh.api.config.MyConstants;
import com.lh.common.entity.admin.User;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.security.SecureRandom;

/**
 * App 抽象 controller
 */
public abstract class AbstractController {

    private static SecureRandom random = new SecureRandom();

    /**
     * 获取用户信息
     *
     * @return
     */
    protected User getUser() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Object object = attributes.getAttribute(MyConstants.USER_INFO_KEY, RequestAttributes.SCOPE_REQUEST);
        //拿到请求头里的语言标识
        return (User) object;
    }

    /**
     * 获取设备ID
     *
     * @return
     */
    protected String getDevice() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String device = request.getHeader("client_id");
        if (StringUtils.isEmpty(device)) {
            device = request.getParameter("client_id");
        }
        return device;
    }

    /**
     * 获取用户userId
     *
     * @return
     */
    protected Long getUserId() {
        User user = getUser();
        if(user == null ){
            return null;
        }
        return user.getUid();
    }

    /**
     * 获取token
     *
     * @return
     */
    protected String getToken() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //拿到请求头里的语言标识
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            token = request.getParameter("token");
        }
        return token;
    }

    protected String getParameter(String key) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //拿到请求头里的语言标识
        HttpServletRequest request = attributes.getRequest();
        return request.getParameter(key);
    }


    /**
     * 产生n位随机数
     *
     * @param n 随机数数量
     * @return n位随机数
     */
    protected String getRandom(int n) {
        // 防止第一为出现0
        StringBuilder result = new StringBuilder(String.valueOf(random.nextInt(8) + 1));
        for (int i = 1; i < n; i++) {
            result.append(random.nextInt(9));
        }
        return result.toString();
    }

    /**
     * 生成11位序号(10位时间戳+3位随机数)
     */
    protected String generateNo() {
        int num = random.nextInt(9);
        //10位时间戳
        String timeStamp = System.currentTimeMillis() / 1000 + "";
        return timeStamp + num;
    }

}
